package com.xianbeiying.app.ckgl.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.activity.ChangePasswordActivity;
import com.xianbeiying.app.ckgl.activity.DateTimeActivity;
import com.xianbeiying.app.ckgl.activity.GhPersonActivity;
import com.xianbeiying.app.ckgl.activity.GhSettingActivity;
import com.xianbeiying.app.ckgl.activity.LoginActivity;
import com.xianbeiying.app.ckgl.activity.MyCollectActivity;
import com.xianbeiying.app.ckgl.activity.MyMessageActivity;
import com.xianbeiying.app.ckgl.activity.TechnicalSupportActivity;
import com.xianbeiying.app.ckgl.bean.LogOutBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.ImageModel;
import com.xianbeiying.app.ckgl.model.LoginModel;
import com.xianbeiying.app.ckgl.photo.util.PhotoPickerIntent;
import com.xianbeiying.app.ckgl.util.SharedPreferencesUtils;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.CircleImageView;
import com.xianbeiying.app.ckgl.widght.popwindow.CenterPopWindow;

import java.util.ArrayList;

/**
 * 我的
 */
public class MyFragment extends BaseFragment implements View.OnClickListener, HttpCallBackUtil {
    private static MyFragment myFragment;
    public static MyFragment getInstance() {
        myFragment = new MyFragment();
        return myFragment;
    }
    private View view;
    private ImageView iv_title_left;
    private TextView tv_title;
    //
    private CircleImageView iv_myhead;
    private TextView tv_myname;
    private ImageView iv_myright;
    private LinearLayout ly_wszr, ly_wsdd, ly_wszl, ly_mycollect, ly_mynews, ly_pwd, ly_jszc, ly_datime, ly_exit;
    //图片上传
    private ImageModel imageModel;
    private LoginModel loginModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, null);
        initView();
        return view;
    }

    private void initView() {
        imageModel = new ImageModel(getActivity());
        imageModel.addResponseListener(this);
        loginModel = new LoginModel(getActivity());
        loginModel.addResponseListener(this);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        iv_title_left = (ImageView) view.findViewById(R.id.iv_title_left);
        iv_myhead = (CircleImageView) view.findViewById(R.id.iv_myhead);
        tv_myname = (TextView) view.findViewById(R.id.tv_myname);
        iv_myright = (ImageView) view.findViewById(R.id.iv_myright);
        ly_wszr = (LinearLayout) view.findViewById(R.id.ly_wszr);
        ly_wsdd = (LinearLayout) view.findViewById(R.id.ly_wsdd);
        ly_wszl = (LinearLayout) view.findViewById(R.id.ly_wszl);
        ly_mycollect = (LinearLayout) view.findViewById(R.id.ly_mycollect);
        ly_mynews = (LinearLayout) view.findViewById(R.id.ly_mynews);
        ly_pwd = (LinearLayout) view.findViewById(R.id.ly_pwd);
        ly_jszc = (LinearLayout) view.findViewById(R.id.ly_jszc);
        ly_datime = (LinearLayout) view.findViewById(R.id.ly_datime);
        ly_exit = (LinearLayout) view.findViewById(R.id.ly_exit);
        tv_title.setText("我的");
        iv_title_left.setVisibility(View.GONE);
        initData();
        initEvent();
    }

    private void initData() {
        LogUtils.e("http://" + MyApplication.IP + UrlBean.IMAGES + MyApplication.loginBean.getUSERURL());
        if (MyApplication.loginBean != null && MyApplication.loginBean.getUSERURL() != null) {
//            Glide.with(getActivity())
//                    .load("http://" + MyApplication.IP + UrlBean.IMAGES + MyApplication.loginBean.getUSERURL())
//                    .centerCrop()
//                    .thumbnail(0.1f)
//                    .error(R.mipmap.login)
//                    .into(iv_myhead);
            ImageLoader.getInstance().displayImage("http://"+MyApplication.IP+UrlBean.IMAGES + MyApplication.loginBean.getUSERURL(),iv_myhead);
        } else {
            iv_myhead.setImageResource(R.mipmap.login);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (MyApplication.loginBean != null) {
            tv_myname.setText(MyApplication.loginBean.getREALNAME());
        }
//        if(imgPaths.size()!=0){
//        }
    }

    private void initEvent() {
        iv_myright.setOnClickListener(this);
        ly_wszr.setOnClickListener(this);
        ly_wsdd.setOnClickListener(this);
        ly_wszl.setOnClickListener(this);
        ly_mycollect.setOnClickListener(this);
        ly_mynews.setOnClickListener(this);
        ly_pwd.setOnClickListener(this);
        ly_jszc.setOnClickListener(this);
        ly_datime.setOnClickListener(this);
        ly_exit.setOnClickListener(this);
    }
    private ArrayList<String> imgPaths = new ArrayList<>();
    private static final int MAX_PIC_NUM = 1;
    private boolean isMulti;
    private CenterPopWindow deletepop;//用户退出

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //设置图像
            case R.id.iv_myright:
                imgPaths.clear();
                PhotoPickerIntent intents = new PhotoPickerIntent(getActivity());
                intents.setPhotoCount(MAX_PIC_NUM - imgPaths.size());
                intents.setMultiChoose(isMulti);
                intents.putExtra("fragment","MyFragment");
                startActivity(intents);
//                startActivityForResult(intents, ADD_CHOOSE);
                break;
            //温室主人
            case R.id.ly_wszr:
                startActivity(new Intent().setClass(getActivity(), GhPersonActivity.class));
                break;
            //温室设置
            case R.id.ly_wszl:
                startActivity(new Intent().setClass(getActivity(), GhSettingActivity.class));
                break;
            //我的收藏
            case R.id.ly_mycollect:
                startActivity(new Intent().setClass(getActivity(), MyCollectActivity.class));
                break;
            //我的消息
            case R.id.ly_mynews:
                startActivity(new Intent().setClass(getActivity(), MyMessageActivity.class));
                break;
            //修改密码
            case R.id.ly_pwd:
                startActivity(new Intent().setClass(getActivity(), ChangePasswordActivity.class));
                break;
            //技术支持
            case R.id.ly_jszc:
                startActivity(new Intent().setClass(getActivity(), TechnicalSupportActivity.class));
                break;
            //日期和时间
            case R.id.ly_datime:
                startActivity(new Intent().setClass(getActivity(), DateTimeActivity.class));
                break;
            //退出
            case R.id.ly_exit:
                deletepop = new CenterPopWindow(getActivity(), R.layout.pop_window_exit);
                deletepop.setTextOnClickListener(R.id.btn_end, this);
                deletepop.setTextOnClickListener(R.id.btn_cancel, this);
                deletepop.setTextOnClickListener(R.id.ly_exitss, this);
                break;
            case R.id.btn_end:
                //退出用户
//                SharedPreferencesUtils.clearUser();
                loginModel.getLogout(true);
                break;
            case R.id.ly_exitss:
            case R.id.btn_cancel:
                deletepop.dismiss();
                break;
        }
    }

    private LogOutBean logOutBean;

    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals("退出")) {
            logOutBean = JSON.parseObject(new String((byte[]) content), LogOutBean.class);
            if (logOutBean.isR()) {
                SharedPreferencesUtils.setStringDate("username", "");
                SharedPreferencesUtils.setStringDate("userpwd", "");
                MyApplication.isLogin = false;
                MyApplication.noticesBean = null;
                MyApplication.jingxunBean = null;
                MyApplication.loginBean = null;
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
                deletepop.dismiss();
            } else {
                ToastUtil.showInfo(getActivity(), "退出失败");
            }
        }
    }
}
